import { defineStore } from 'pinia'
import { store } from '@/stores'

export const useUserStore = defineStore({
  id: 'live-broadcast-user',
  state: () => ({
		isLogin: false,
    userinfo:{
			id: 0,
			created_at: '',
			updated_at: null,
			deleted_at: null,
			account: '0000000000',
			nickname: '未登录',
			email: null,
			phone: null,
			date_birth: 0,
			is_login: 0,
			login_time: null,
			login_out_time: null,
			identity: null,
			device_info: null,
			token: '',
			avatar: ''
		},
		noIsAvatar: '',
		concern: 0,
		friend: 0
  }),
  actions: {
		SET_USERINFO(userinfo) {
			this.isLogin = true
			this.userinfo = userinfo
			uni.setStorage({
				key: 'USER_INFO',
				data: JSON.stringify({
					...this.userinfo,
				}),
			})
			uni.setStorage({
				key: 'TOKEN',
				data: JSON.stringify(userinfo.token),
			})
		},
		SET_REMOVE() {
			this.isLogin = false
			this.userinfo = {
				id: 0,
				created_at: '',
				updated_at: null,
				deleted_at: null,
				account: '0000000000',
				nickname: '未登录',
				email: null,
				phone: null,
				date_birth: 0,
				is_login: 0,
				login_time: null,
				login_out_time: null,
				identity: null,
				device_info: null,
				token: '',
				avatar: ''
			}
			uni.removeStorage({
				key: 'USER_INFO',
				success: (res) => {}
			});
			uni.removeStorage({
				key: 'TOKEN',
				success: (res) => {}
			});
		},
		SET_NICKNAME(nickname) {
			this.userinfo.nickname = nickname
			uni.setStorage({
				key: 'USER_INFO',
				data: JSON.stringify({
					...this.userinfo,
				}),
			})
		},
		SET_EMAIL(email) {
			this.userinfo.email = email
			uni.setStorage({
				key: 'USER_INFO',
				data: JSON.stringify({
					...this.userinfo,
				}),
			})
		},
		SET_PHONE(phone) {
			this.userinfo.phone = phone
			uni.setStorage({
				key: 'USER_INFO',
				data: JSON.stringify({
					...this.userinfo,
				}),
			})
		},
		SET_AVATAR(avatar) {
			this.userinfo.avatar = avatar
			uni.setStorage({
				key: 'USER_INFO',
				data: JSON.stringify({
					...this.userinfo,
				}),
			})
		},
		SET_DATE_BIRTH(date_birth) {
			this.userinfo.date_birth = date_birth
			uni.setStorage({
				key: 'USER_INFO',
				data: JSON.stringify({
					...this.userinfo,
				}),
			})
		},
    SET_CONCERN(concern) {
      this.concern = concern
    },
		SET_FRIEND(friend) {
		  this.friend = friend
		}
  }
})

export function useUserStoreHook() {
  return useUserStore(store)
}
